package com.lanou.dllo.soccernews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.greendao.Users;
import com.lanou.dllo.soccernews.database.UsersDBhelper;
import com.lanou.dllo.soccernews.utils.RegularUtil;

import java.util.List;
import java.util.Random;


public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private Button check, register;
    private EditText tel, checknum;
    private String telString, checknumString;
    private String[] nums = {"12344", "47488", "78891", "89978", "76654"};
    private boolean isTel = false;//检查电话是否已经被注册过
    private boolean isTelNum = false;//检查电话的格式是否正确
    private UsersDBhelper help;
    private Random random;
    private int min = 0;
    private int max = nums.length;
    private String num = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
        check.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    private void init() {
        check = (Button) findViewById(R.id.check);
        register = (Button) findViewById(R.id.register);

        tel = (EditText) findViewById(R.id.tel);
        checknum = (EditText) findViewById(R.id.checkmnum);
        random = new Random();
        help = UsersDBhelper.getInstance();
    }


    @Override
    public void onClick(View v) {
        telString = tel.getText().toString();
        switch (v.getId()) {
            case R.id.check:
                help.deleteOne();
                int i = random.nextInt(max) % (max - min + 1) + min;
                num = nums[i];
                isTel = help.getTel(telString);
                isTelNum = RegularUtil.checkPhone(this, telString);
                Log.i("lanou", isTel + "快快快快快");
                if (isTelNum == true) {
                    if (isTel == true) {
                        SmsManager smsManager = SmsManager.getDefault();
                        if (num.length() > 0) {
                            List<String> contents = smsManager.divideMessage(num);
                            for (String sms : contents) {
                                smsManager.sendTextMessage(telString, null, sms, null, null);
                            }
                            Toast.makeText(this, "发送成功!", Toast.LENGTH_SHORT).show();

                        } else {
                            smsManager.sendTextMessage(telString, null, num, null, null);

                        }
                    } else {

                        Toast.makeText(this, help.getUsersInfoList().toString() + "您的账号已经被注册过咯!", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(this, "您输入的电话号码不符合", Toast.LENGTH_LONG).show();
                }

                break;

            case R.id.register:
                checknumString = checknum.getText().toString();
                if (checknumString.equals(num)) {
                    Users users = new Users(telString);
                    help.addToUserInfoTable(users);
                    Intent intent = new Intent(this, AddRegisterActivity.class);
                    intent.putExtra("tel", telString);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(this, "验证没有成功哦", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }
}
